package ptij.vocoder.baseModel.preprocess;

import ptolemy.actor.*;
import ptolemy.kernel.*;
import ptolemy.kernel.util.*;
import ptolemy.data.ObjectToken;
import ptolemy.data.BooleanToken;
import ptolemy.data.type.*;

import ptij.vocoder.baseModel.*;

//////////////////////////////////////////////////////////////////////////
//// Homing
/** 

@author Ivan Jeukens
@version v 0.1 07/08/2003
*/
public class Homing extends TypedAtomicActor {

    public Homing(CompositeEntity container, String name) throws
            IllegalActionException, NameDuplicationException {
        super(container, name);

        input = new TypedIOPort(this, "input", true, false);
        input.setTypeEquals(BaseType.OBJECT);

        reset = new TypedIOPort(this, "reset", false, true);
        reset.setTypeEquals(BaseType.BOOLEAN);
    }

    ///////////////////////////////////////////////////////////////////
    ////                     ports and parameters                  ////

    // Input ports
    public TypedIOPort input;

    // Output Ports
    public TypedIOPort reset;

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////

    public void initialize() throws IllegalActionException {
        super.initialize();
        _flag = false;
    }

    public void fire() throws IllegalActionException {
        if(_flag) {
            reset.broadcast(new BooleanToken(true));
        }

        ObjectToken obj = (ObjectToken) input.get(0);
        Short [] frames = (Short []) obj.getValue();

        _homing(frames);
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////

    private void _homing(Short [] frames) {
        _flag = false;
        for(int i = 0;i < Cnst.L_FRAME;i++) {
            if(frames[i].shortValue() == 1) {
                _flag = true;
            }
        }
    }

    ///////////////////////////////////////////////////////////////////
    ////                         private variables                 ////

    private boolean _flag;
}
